requireJS(['constant', 'site', 'handlebars', 'public'], function (Constant, site, Handlebars, Public) {

    var url = {
        selectTargetManagementList: "manager/selectTargetManagementList.do",
        insertTargetManagementInfo: "manager/insertTargetManagementInfo.do",
        Loanedecord:"manager/Loanedecord.do"
    }

    var frontData = {
        pageNum: 1
    }

    var product_id;
    var order_id;
    var taskId;
    var node_type;
    var node_id;
    var s_id;
    var ari;
    var status;
    var time;
    var full_mark_time;
    var repayment_way;
    var at_id;
    var apply_money;

    var frontData = {
        pageNum: 1
    }

    //查询标的列表
    function salesManCheck() {
        var data = {
            pageNum: frontData.pageNum,
        }
        if ($("#beginTime").val()) {
            data.beginTime = $("#beginTime").val()
        }
        if ($("#endTime").val()) {
            data.endTime = $("#endTime").val()
        }
        if ($("#seleNames option:selected").val()) {
            data.type = $("#seleNames option:selected").val()
        }
        if ($("#inputNames").val()) {
            data.keyword = $("#inputNames").val();
        }
        if ($("#at_id option:selected").val()) {
            data.atId = $("#at_id option:selected").val()
        }
        if ($("#loan_way option:selected").val()) {
            data.loan_way = $("#loan_way option:selected").val()
        }
        if ($("#product_choose_list").val()) {
            data.product_id = $("#product_choose_list").val()
        }
        site.send({
            url: url.selectTargetManagementList,
            data: data,
            callback: function (data) {
                data = JSON.parse(data)
                if (data.type == 1) {
                    if (data.content) {
                        $('.target-page-container').createPager({
                            current: frontData.pageNum,
                            total: data.content.total,
                            pageSize: data.content.pageSize,
                            needInput: false,
                            backFn: function (current) {
                                frontData.pageNum = current;
                                salesManCheck();
                            }
                        })
                        $('.result').html(data.content.total ? data.content.total : 0);
                    }
                    //handlebars模板引擎
                    var source = $("#target_list_tpl").html()
                    var template = Handlebars.compile(source)
                    var content = data.content;
                    var menuIndex = getAurlparam('h') || 0;
                    var thirdMenusIndex = getAurlparam('t') || 0;
                    var leftIndex = getAurlparam('i') || 0;

                    var storageContent = JSON.parse(localStorage.getItem('content'));

                    var obj = {
                        content: content,
                        fourthMenus: storageContent.menus[menuIndex].children[leftIndex].children[thirdMenusIndex].children || {}
                    }
                    var html = template(obj)
                    $('#target_list').html(html)

                    //复选框一个没选中则全选去除
                    $.each($("input[name='ch_id']"), function (i, e) {
                        var flag = false;
                        $(e).on('click', function () {
                            var _checked = $("input[name='ch_id_all']")
                            var _checkedAll = $("input[name='ch_id']")
                            $.each(_checkedAll, function (ii, ee) {
                                if ($(ee).prop('checked')) {
                                    flag = true
                                }
                                else {
                                    flag = false
                                    return false;
                                }
                            })
                            _checked.prop('checked', flag);
                        })
                    })


                    //渲染出模板后在进行操作
                    $(".operations").on('change', function () {
                        product_id = $(this).attr('product_id');
                        order_id = $(this).attr('order_id');
                        taskId = $(this).attr('task_id');
                        node_type = $(this).attr('node_type')
                        node_id = $(this).attr('node_id')
                        s_id = $(this).attr('s_id')
                        ari = $(this).attr('ari');
                        status = $(this).attr('status');
                        time = $(this).attr('time');
                        full_mark_time = $(this).attr('full_mark_time');
                        repayment_way = $(this).attr('repayment_way');
                        at_id = $(this).attr('at_id');
                        apply_money = $(this).attr('apply_money');
                        if ($(this).val() === 'checkOrder') {
                            pjaxGo("loanBefore/targetDetail.html?order_id=" + order_id + "&product_id=" + product_id + "&ari=" + ari +"&status=" + status + '&time=' + time + '&full_mark_time=' + full_mark_time + '&repayment_way=' + repayment_way);
                        }
                        if ($(this).val() === 'investNow') {
                            $("#investment_amount").val('');
                            $(".invest_winBox").show();
                        }
                        if ($(this).val() === 'loanedRecord') {
                            queryLoanedRecord(frontData);
                            $('.loanedRecord_winBox').show();
                        }
                        $(this).val(-1);
                    })

                    Public.record('t_order_public');

                } else {
                    Constant.fn.Message("F", data.msg);
                }
            },
            callError: function () {
                alert("error")
            }
        })
    }

    //查询出借记录
    function queryLoanedRecord(frontData){
        site.send({
            url: url.Loanedecord,
            data: {
                oid: order_id
            },
            callback: function (data) {
                data = JSON.parse(data)
                if (data.type == 1) {
                    var source = $("#loanedRecord_list_tpl").html();
                    var template = Handlebars.compile(source);
                    var list = data.content;
                    var html = template(list);
                    $('#loanedRecord_list').html(html);
                    //分页
                    $('.loanedRecord-page-container').createPager({
                        current: frontData.pageNum,
                        total: data.content.total,
                        pageSize: data.content.pageSize,
                        needInput: false,
                        backFn: function (current) {
                            frontData.pageNum = current;
                            queryLoanedRecord(frontData);
                        }
                    })
                } else {
                    Constant.fn.Message("F", data.msg);
                }
            },
            callError: function () {
                alert("error")
            }
        })
    }

    salesManCheck();

    $('#sureInvest').on('click', function () {
        var investment_amount = Constant.fn.toNum($("#investment_amount").val());
        //var investment_amount = $("#investment_amount").val();
        if (!investment_amount) {
            Constant.fn.Message("I", "请输入金额");
            return;
        }
        var data = {
            oid: order_id,
            investment_amount: investment_amount,
            at_id: at_id,
            apply_money: apply_money
        }
        site.send({
            url: url.insertTargetManagementInfo,
            data: data,
            callback: function (data) {
                data = JSON.parse(data);
                if(data.type == '1'){
                    Constant.fn.Message("T", data.msg);
                    $('.invest_winBox').hide();
                }else{
                    Constant.fn.Message("F", data.msg);
                }
            }
        })
    })

    $('#investment_amount').on({
        'focus': function () {
            $(this).val(Constant.fn.toNum($(this).val()));
        },
        'blur': function () {
            $(this).val(Constant.fn.toRMB($(this).val()));
        }
    })

})